package adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import bean.BooksBean;
import jiyun.com.yiyanmeng.R;

/**
 * Created by 裘翔 on 2019/11/6.
 */

public class BooksRecAdaper extends RecyclerView.Adapter<BooksRecAdaper.ViewHolder>{
    private Context context;
    private ArrayList<BooksBean> list;

    public BooksRecAdaper(Context context, ArrayList<BooksBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public BooksRecAdaper.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.books_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BooksRecAdaper.ViewHolder holder, final int position) {
        Glide.with(context).load(list.get(position).getPic()).into(holder.books_iv);
        holder.books_name.setText(list.get(position).getName());
        holder.books_monery.setText(list.get(position).getXian_price());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickItem.setonClickItemListener(v,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView books_iv;
        TextView books_name;
        TextView books_monery;
        public ViewHolder(View itemView) {
            super(itemView);
            books_iv=itemView.findViewById(R.id.books_iv);
            books_name=itemView.findViewById(R.id.books_name);
            books_monery=itemView.findViewById(R.id.books_monery);
        }
    }
    public OnClickItem onClickItem;

    public void setOnClickItem(OnClickItem onClickItem) {
        this.onClickItem = onClickItem;
    }

    public interface OnClickItem{
        void setonClickItemListener(View v, int position);
    }
}
